package com.bysj.servelt;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bysj.model.Evaluation;
import com.bysj.model.Goods;
import com.bysj.model.OrderEvaluation;
import com.bysj.model.User;
import com.bysj.service.EvaluationService;
import com.bysj.service.OrderService;

/**
 * Servlet implementation class OrderAddevaluation
 */
@WebServlet("/OrderAddevaluation")
public class OrderAddevaluationSetvlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private EvaluationService eService = new EvaluationService(); 
	private OrderService oService = new OrderService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderAddevaluationSetvlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User u = (User) request.getSession().getAttribute("user");
		if (u == null) {
			request.getRequestDispatcher("/user_login.jsp").forward(request, response);
			return;
		}
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
		StringBuilder responseStrBuilder = new StringBuilder();
		String inputStr;

		while ((inputStr = streamReader.readLine()) != null)
		    responseStrBuilder.append(inputStr);
        Map<String ,Object> map = JSONObject.parseObject(responseStrBuilder.toString(),Map.class);
        int orderId = Integer.valueOf(String.valueOf(map.get("orderId")));
		ArrayList<Evaluation> obj=JSON.parseObject(map.get("evaluations").toString(), new TypeReference<ArrayList<Evaluation>>() { });
//		JSONArray arr=obj.getJSONArray("evaluations");
//		String js=JSONObject.toJSONString(arr, SerializerFeature.WriteClassName);//将array数组转换成字符串
		List<Evaluation>  evaluations = obj;//把字符串转换成集合
		OrderEvaluation o = new OrderEvaluation();
		o.setEvaluations(evaluations);
		eService.addEvaluation(o,u);
		oService.updateStatus(orderId, 5);
		request.getRequestDispatcher("/order_list?stattus=6").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
